<?php

require_once("./View/NavigationView.php");
require_once("./View/ApplicationView.php");
require_once("LoginController.php");
require_once("RegisterController.php");
require_once("UserController.php");

require_once("./Model/LoginModel.php");
require_once("./Model/DALBase.php");

class Application {
	
	/**
	 * @var NavigationView
	 */
	private $navView;
	/**
	 * @var LoginView
	 */
	private $loginView;
	/**
	 * @var RegisterView
	 */
	private $regView;
	/**
	 * @var UserPageView
	 */
	private $userPageView;
	/**
	 * @var ApplicationView
	 */
	private $appView;
	/**
	 * @var LoginController
	 */
	private $loginController;
	/**
	 * @var RegisterController
	 */
	private $regController;
	/**
	 * @var UserController
	 */
	private $userController;
	/**
	 * @var LoginModel
	 */
	private $loginModel;
	/**
	 * @var DALBase
	 */
	private $DALBase;
	
	public function __construct() {
		$this->navView = 		new NavigationView();
		$this->loginView = 		new LoginView($this->navView);
		$this->regView = 		new RegisterView($this->navView);
		$this->userPageView = 	new UserPageView($this->navView);
		$this->appView = 		new ApplicationView($this->navView);
		
		$this->loginModel =		new LoginModel();
		$this->DALBase = 		new DALBase();
		
		$this->loginController = new LoginController($this->navView,
													 $this->loginView, 
													 $this->loginModel,
													 $this->DALBase);
	    $this->regController =	 new RegisterController($this->navView,
	    												$this->regView,
														$this->DALBase);
	}
	
	public function runApplication() {
		$this->loginController->handleInput();
		if ($this->loginModel->userIsLoggedIn()) {
			$user = $this->loginModel->getUserObject();
			$userList = $this->DALBase->getAllUsers();
			
			$this->userController = new UserController($this->navView,
												   	   $this->userPageView,
													   $user,
													   $this->DALBase);
		    $this->userController->handleInput();
			$header = $this->appView->getHeaderHTML($user,
													$this->loginView->getLogoutHTML(),
													$this->navView->getDropDownBox($userList));
		    return $header . $this->appView->getUserPageHTML($this->userController->loadUserPage());
		}
		if ($this->navView->userWantsToShowRegister()) {
			$this->regController->handleInput();
			return $this->appView->getRegisterHTML($this->regView->getRegisterUserContent());
		}
		else {
			return $this->appView->getLoginHTML($this->loginView->getLoginHTML());
		}
	}
}